{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #探险营地"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: adventureCamp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #探险营地"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "探险家小扣的行动轨迹，都将保存在记录仪中。`expeditions[i]` 表示小扣第 `i` 次探险记录，用一个字符串数组表示。其中的每个「营地」由大小写字母组成，通过子串 `->` 连接。\n",
    "> 例：\"Leet->code->Campsite\"，表示到访了 \"Leet\"、\"code\"、\"Campsite\" 三个营地。\n",
    "\n",
    "`expeditions[0]` 包含了初始小扣已知的所有营地；对于之后的第 `i` 次探险(即 `expeditions[i]` 且 i > 0)，如果记录中包含了之前均没出现的营地，则表示小扣 **新发现** 的营地。\n",
    "\n",
    "请你找出小扣发现新营地最多且索引最小的那次探险，并返回对应的记录索引。如果所有探险记录都没有发现新的营地，返回 `-1`\n",
    "\n",
    "**注意：**\n",
    "- 大小写不同的营地视为不同的营地；\n",
    "- 营地的名称长度均大于 `0`。\n",
    "\n",
    "**示例 1：**\n",
    ">输入：`expeditions = [\"leet->code\",\"leet->code->Campsite->Leet\",\"leet->code->leet->courier\"]`\n",
    ">\n",
    ">输出：`1`\n",
    ">\n",
    ">解释：\n",
    ">初始已知的所有营地为 \"leet\" 和 \"code\"\n",
    ">第 1 次，到访了 \"leet\"、\"code\"、\"Campsite\"、\"Leet\"，新发现营地 2 处：\"Campsite\"、\"Leet\"\n",
    ">第 2 次，到访了 \"leet\"、\"code\"、\"courier\"，新发现营地 1 处：\"courier\"\n",
    ">第 1 次探险发现的新营地数量最多，因此返回 `1`\n",
    "\n",
    "**示例 2：**\n",
    ">输入：`expeditions = [\"Alice->Dex\",\"\",\"Dex\"]`\n",
    ">\n",
    ">输出：`-1`\n",
    ">\n",
    ">解释：\n",
    ">初始已知的所有营地为 \"Alice\" 和 \"Dex\"\n",
    ">第 1 次，未到访任何营地；\n",
    ">第 2 次，到访了 \"Dex\"，未新发现营地；\n",
    ">因为两次探险均未发现新的营地，返回 `-1`\n",
    "\n",
    "**示例 3：**\n",
    ">输入：`expeditions = [\"\",\"Gryffindor->Slytherin->Gryffindor\",\"Hogwarts->Hufflepuff->Ravenclaw\"]`\n",
    ">\n",
    ">输出：`2`\n",
    ">\n",
    ">解释：\n",
    ">初始未发现任何营地；\n",
    ">第 1 次，到访 \"Gryffindor\"、\"Slytherin\" 营地，其中重复到访 \"Gryffindor\" 两次，\n",
    ">因此新发现营地为 2 处：\"Gryffindor\"、\"Slytherin\"\n",
    ">第 2 次，到访 \"Hogwarts\"、\"Hufflepuff\"、\"Ravenclaw\" 营地；\n",
    ">新发现营地 3 处：\"Hogwarts\"、\"Hufflepuff\"、\"Ravenclaw\"；\n",
    ">第 2 次探险发现的新营地数量最多，因此返回 `2`\n",
    "\n",
    "**提示：**\n",
    "- `1 <= expeditions.length <= 1000`\n",
    "- `0 <= expeditions[i].length <= 1000`\n",
    "- 探险记录中只包含大小写字母和子串\"->\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [0Zeoeg](https://leetcode.cn/problems/0Zeoeg/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [0Zeoeg](https://leetcode.cn/problems/0Zeoeg/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"leet->code\",\"leet->code->Campsite->Leet\",\"leet->code->leet->courier\"]', '[\"Alice->Dex\",\"\",\"Dex\"]', '[\"\",\"Gryffindor->Slytherin->Gryffindor\",\"Hogwarts->Hufflepuff->Ravenclaw\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions) -> int:\n",
    "        Known_area = set(expeditions[0].split('->'))\n",
    "        ans = 0\n",
    "        idx = -1\n",
    "        n = len(expeditions)\n",
    "        for i in range(1, n):\n",
    "            num = 0\n",
    "            areas = set(expeditions[i].split('->'))\n",
    "            for area in areas:\n",
    "                if area not in Known_area and area != '':\n",
    "                    num += 1\n",
    "            Known_area.update(areas)\n",
    "            if num > ans:\n",
    "                idx = i\n",
    "                ans = num\n",
    "        return idx\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        r = 0\n",
    "        ret = -1\n",
    "        def get(aExp):\n",
    "            if \"->\" in aExp:\n",
    "                l = aExp.split('->')\n",
    "                return set(l)\n",
    "            if len(aExp) > 0:\n",
    "                return set([aExp])\n",
    "            else:\n",
    "                return set()\n",
    "        e = get(expeditions[0])\n",
    "        for i in range(1, len(expeditions)):\n",
    "            n = get(expeditions[i])\n",
    "           \n",
    "            d = len(n - e)\n",
    "            # print(f\"e {e} n {n} d {d} {n - e}\")\n",
    "            if d >= r:\n",
    "                if d > r:\n",
    "                    r = d\n",
    "                    ret = i\n",
    "            e |= n\n",
    "            \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        # 初始化已知营地集合\n",
    "        known_camps = set(expeditions[0].split('->')) if expeditions[0] else set()\n",
    "\n",
    "        max_new_camps = 0\n",
    "        best_index = -1\n",
    "\n",
    "        # 从第二个记录开始遍历\n",
    "        for idx, record in enumerate(expeditions[1:], 1):\n",
    "            camps = set(record.split('->')) if record else set()\n",
    "            new_camps_count = sum(1 for camp in camps if camp not in known_camps)\n",
    "\n",
    "            # 更新已知营地集合\n",
    "            known_camps.update(camps)\n",
    "\n",
    "            # 如果这次探险新发现的营地数量比目前最多的还要多\n",
    "            if new_camps_count > max_new_camps:\n",
    "                max_new_camps = new_camps_count\n",
    "                best_index = idx\n",
    "\n",
    "        return best_index if max_new_camps > 0 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        dic = defaultdict(str)\n",
    "        dic[''] = 1\n",
    "        for i in expeditions[0].split(\"->\") :\n",
    "            #print(i)\n",
    "            dic[i] = 1\n",
    "        ans = 0\n",
    "        ansidx = -1\n",
    "        a = 0\n",
    "        for i in range(1, len(expeditions)) :\n",
    "            a = 0 \n",
    "            for j in expeditions[i].split(\"->\") : \n",
    "                #print(j)\n",
    "                if not dic.get(j) : \n",
    "                    a += 1\n",
    "                    dic[j] = 1\n",
    "            if a > ans :\n",
    "                ans = a \n",
    "                ansidx = i\n",
    "        return ansidx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        \n",
    "        k=len(expeditions)\n",
    "        \n",
    "        t=expeditions[0].split('->')\n",
    "        a=set(t) \n",
    "        ans=index=-1\n",
    "        \n",
    "        a.add('')\n",
    "        big=0\n",
    "        for i in range(1,k):\n",
    "            w=expeditions[i].split('->')\n",
    "            w=set(w)\n",
    "            t=len(w-a)\n",
    "            if t>big:\n",
    "                big=t\n",
    "                ans=i\n",
    "            a|=w\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        s = set(expeditions[0].split('->'))\n",
    "        n = len(expeditions)\n",
    "        ans = -1\n",
    "        pre = 0\n",
    "        for i in range(1, n):\n",
    "            ts = set()\n",
    "            for ex in expeditions[i].split('->'):\n",
    "                if ex not in s and len(ex): ts.add(ex)\n",
    "            print(ts, len(ts), len(ts) > pre)\n",
    "            s |= ts\n",
    "            if len(ts) > pre:\n",
    "                ans = i\n",
    "                pre = len(ts)\n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, ee: List[str]) -> int:\n",
    "        k=len(ee)\n",
    "        t=ee[0].split('->')\n",
    "        a=set(t) \n",
    "        ans=index=-1\n",
    "        a.add('')\n",
    "        big=0\n",
    "        for i in range(1,k):\n",
    "            w= set(ee[i].split('->'))\n",
    "            if (t:=len(w-a))>big:\n",
    "                big=t\n",
    "                ans=i\n",
    "            a|=w\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, ee: List[str]) -> int:\n",
    "        \n",
    "        k=len(ee)\n",
    "        \n",
    "        t=ee[0].split('->')\n",
    "        a=set(t) \n",
    "        ans=index=-1\n",
    "        \n",
    "        a.add('')\n",
    "        big=0\n",
    "        for i in range(1,k):\n",
    "            w= set(ee[i].split('->'))\n",
    "          \n",
    "            \n",
    "            if (t:=len(w-a))>big:\n",
    "                big=t\n",
    "                ans=i\n",
    "            a|=w\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        know_list = set(expeditions[0].split(\"->\"))\n",
    "        print(know_list)\n",
    "        max_len = 0\n",
    "        res = -1\n",
    "        index = 1\n",
    "        for exp in expeditions[1:]:\n",
    "            if exp == \"\":\n",
    "                continue\n",
    "            new_exp = set(exp.split(\"->\"))\n",
    "            covert = len(know_list & new_exp)\n",
    "            # print(f\"new_exp:{new_exp},cover:{covert} len:{len(new_exp)}, max_len:{max_len}, res:{res}\")\n",
    "            if max_len < len(new_exp) - covert:\n",
    "                res = index\n",
    "                max_len = len(new_exp) - covert\n",
    "            index += 1\n",
    "            # 将本次探险的新营地加到探索过的营地中\n",
    "            know_list = know_list | new_exp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        old = expeditions[0].split(\"->\")\n",
    "        dic = dict()\n",
    "        for d in old:\n",
    "            dic[d] = 1\n",
    "        res = -1\n",
    "        mx = 0\n",
    "        for i, e in enumerate(expeditions[1:]):\n",
    "            new = e.split(\"->\")\n",
    "            w = 0\n",
    "            for  n in new:\n",
    "                if len(n)==0:\n",
    "                    continue\n",
    "                if dic.get(n) is None:\n",
    "                    dic[n] = 1\n",
    "                    w += 1\n",
    "            if w>mx:\n",
    "                res = i+1\n",
    "                mx = w\n",
    "        return res\n",
    "            \n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        max_index = 0\n",
    "        max_counter = 0\n",
    "        exp = set(expeditions[0].split(\"->\"))\n",
    "        for i, e in enumerate(expeditions[1:]):\n",
    "            if e == \"\":\n",
    "                continue\n",
    "            c = 0\n",
    "            for name in e.split(\"->\"):\n",
    "                if name not in exp:\n",
    "                    exp.add(name)\n",
    "                    c += 1\n",
    "            if c > max_counter:\n",
    "                max_counter = c\n",
    "                max_index = i\n",
    "        return max_index + 1 if max_counter > 0 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        a = expeditions\n",
    "        ans = 0\n",
    "        ansI = -1\n",
    "        data = set(a[0].split(\"->\"))\n",
    "        if \"\" in data:\n",
    "                data.remove(\"\")\n",
    "        for i in range(1, len(a)):\n",
    "            d = set(a[i].split(\"->\"))\n",
    "            if \"\" in d:\n",
    "                d.remove(\"\")\n",
    "            c = 0\n",
    "            for j in d:\n",
    "                if j not in data:\n",
    "                    c += 1\n",
    "                    data.add(j)\n",
    "            if c > ans:\n",
    "                ans = c\n",
    "                ansI = i\n",
    "        return ansI\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        s = set()\n",
    "        for c in self.string_to_list(expeditions[0]):\n",
    "            s.add(c)\n",
    "        res = -1\n",
    "        max_find = 0\n",
    "        for i in range(1, len(expeditions)):\n",
    "            print(f'i: {i}')\n",
    "            if len(expeditions[i]) == 0:\n",
    "                continue\n",
    "            cur = 0\n",
    "            for c in self.string_to_list(expeditions[i]):\n",
    "                if c not in s:\n",
    "                    cur += 1\n",
    "                    s.add(c)\n",
    "            if cur > max_find:\n",
    "                max_find = cur\n",
    "                res = i\n",
    "            print(f'cur: {cur}')\n",
    "        return res\n",
    "\n",
    "    def string_to_list(self, e):\n",
    "        return e.split('->')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        s = set()\n",
    "        for c in self.string_to_list(expeditions[0]):\n",
    "            s.add(c)\n",
    "        res = -1\n",
    "        max_find = 0\n",
    "        for i in range(1, len(expeditions)):\n",
    "            # print(f'i: {i}')\n",
    "            if len(expeditions[i]) == 0:\n",
    "                continue\n",
    "            cur = 0\n",
    "            for c in self.string_to_list(expeditions[i]):\n",
    "                if c not in s:\n",
    "                    cur += 1\n",
    "                    s.add(c)\n",
    "            if cur > max_find:\n",
    "                max_find = cur\n",
    "                res = i\n",
    "            # print(f'cur: {cur}')\n",
    "        return res\n",
    "\n",
    "    def string_to_list(self, e):\n",
    "        return e.split('->')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        vis = []\n",
    "        for i in expeditions[0].split('->'):\n",
    "            if i == \"\":\n",
    "                continue\n",
    "            vis.append(i)\n",
    "        cao = []\n",
    "\n",
    "        for i,v in enumerate(expeditions):\n",
    "            if v==\"\":\n",
    "                cao.append(0)\n",
    "                continue\n",
    "            temp = v.split('->')\n",
    "            temp = list(set(temp))\n",
    "            cao.append(len(temp)-len(list(set(vis) & set(temp))))\n",
    "            vis.extend(list(set(temp)-(set(temp) & set(vis))))\n",
    "        if sum(cao) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return cao.index(max(cao))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        seen = set()\n",
    "        res = max_val = 0\n",
    "        for i, e in enumerate(expeditions):\n",
    "            if not e:\n",
    "                continue\n",
    "            camps = list(e.split('->'))\n",
    "            count = 0 \n",
    "            \n",
    "            for c in camps:\n",
    "                if c not in seen:\n",
    "                    count += 1\n",
    "                    seen.add(c)\n",
    "            # print(camps, count)\n",
    "            if i > 0 and count > max_val:\n",
    "                max_val = count\n",
    "                res = i \n",
    "        return res if res else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        vis = []\n",
    "        for i in expeditions[0].split('->'):\n",
    "            if i == \"\":\n",
    "                continue\n",
    "            vis.append(i)\n",
    "        cao = []\n",
    "\n",
    "        for i,v in enumerate(expeditions):\n",
    "            if v==\"\":\n",
    "                cao.append(0)\n",
    "                continue\n",
    "            temp = v.split('->')\n",
    "            temp = list(set(temp))\n",
    "            cao.append(len(temp)-len(list(set(vis) & set(temp))))\n",
    "            vis.extend(list(set(temp)-(set(temp) & set(vis))))\n",
    "        if sum(cao) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return cao.index(max(cao))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        find = set(expeditions[0].split(\"->\"))\n",
    "        nums=0\n",
    "        res=-1\n",
    "        for i in range(1,len(expeditions)):\n",
    "            count = 0\n",
    "            if expeditions[i]==\"\":\n",
    "                continue\n",
    "            for item in expeditions[i].split(\"->\"):\n",
    "                if item not in find:\n",
    "                    count+=1\n",
    "                    find.add(item)\n",
    "            if count>nums:\n",
    "                nums=count\n",
    "                res=i\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        ans = 0\n",
    "        result = -1\n",
    "        n = len(expeditions)\n",
    "        cnt0 = set(expeditions[0].split(\"->\"))\n",
    "        for i in range(1,n):\n",
    "            if expeditions[i]==\"\":\n",
    "                continue\n",
    "            c = 0\n",
    "            cnt1 = set(expeditions[i].split(\"->\"))\n",
    "            \n",
    "            for temp in cnt1:\n",
    "                if temp not in cnt0:\n",
    "                    cnt0.add(temp)\n",
    "                    c+=1\n",
    "            if c>ans:\n",
    "                ans = c\n",
    "                result = i\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        vis = []\n",
    "        for i in expeditions[0].split('->'):\n",
    "            if i == \"\":\n",
    "                continue\n",
    "            vis.append(i)\n",
    "        cao = []\n",
    "\n",
    "        for i,v in enumerate(expeditions):\n",
    "            if v==\"\":\n",
    "                cao.append(0)\n",
    "                continue\n",
    "            temp = v.split('->')\n",
    "            temp = list(set(temp))\n",
    "            cao.append(len(temp)-len(list(set(vis) & set(temp))))\n",
    "            vis.extend(list(set(temp)-(set(temp) & set(vis))))\n",
    "        if sum(cao) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return cao.index(max(cao))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        already_know = set(expeditions[0].split('->'))\n",
    "        max_num, ans = 0, -1\n",
    "        for i in range(1, len(expeditions)):\n",
    "            cnt = 0\n",
    "            if expeditions[i] == '':\n",
    "                continue\n",
    "            for camp in expeditions[i].split('->'):\n",
    "                if camp not in already_know:\n",
    "                    already_know.add(camp)\n",
    "                    cnt += 1\n",
    "            if cnt > max_num:\n",
    "                max_num, ans = cnt, i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        visited = set(expeditions[0].split(\"->\")) if len(expeditions[0]) > 0 else set()\n",
    "        max_new, max_index = 0, -1\n",
    "        for i in range(1, len(expeditions)):\n",
    "            camps = expeditions[i].split(\"->\")\n",
    "            num_new = 0\n",
    "            for camp in camps:\n",
    "                if len(camp) > 0 and camp not in visited:\n",
    "                    visited.add(camp)\n",
    "                    num_new += 1\n",
    "            if num_new > max_new:\n",
    "                max_new = num_new\n",
    "                max_index = i\n",
    "        return max_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        start=expeditions[0].split(\"->\")\n",
    "        sstart=set(start)\n",
    "        res=0\n",
    "        ans=-1\n",
    "        for i in range(1,len(expeditions)):\n",
    "            temp=expeditions[i].split(\"->\")\n",
    "            cnt=0\n",
    "            for ss in temp:\n",
    "                if ss not in sstart and ss!=\"\":\n",
    "                    sstart.add(ss)\n",
    "                    cnt+=1\n",
    "            if cnt>res:\n",
    "                res=cnt\n",
    "                ans=i\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        res = -1\n",
    "        t = 0\n",
    "        n = len(expeditions)\n",
    "        e = set(expeditions[0].split('->'))\n",
    "        for i in range(1, n):\n",
    "            cnt = 0\n",
    "            temp = expeditions[i].split('->')\n",
    "            for ex in temp:\n",
    "                if ex and ex not in e:\n",
    "                    cnt += 1\n",
    "                    e.add(ex)\n",
    "            if cnt > t:\n",
    "                res = i\n",
    "                t = cnt\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        # seen = set()\n",
    "        # res = max_val = 0\n",
    "        # for i, e in enumerate(expeditions):\n",
    "        #     if not e:\n",
    "        #         continue\n",
    "        #     camps = list(e.split('->'))\n",
    "        #     count = 0             \n",
    "        #     for c in camps:\n",
    "        #         if c not in seen:\n",
    "        #             count += 1\n",
    "        #             seen.add(c)\n",
    "        #     if i > 0 and count > max_val:\n",
    "        #         max_val = count\n",
    "        #         res = i \n",
    "        # return res if res else -1\n",
    "\n",
    "        # -------------------------------\n",
    "        seen = set(expeditions[0].split('->'))\n",
    "        res, max_val = -1, 0\n",
    "        for i in range(1, len(expeditions)):\n",
    "            if not expeditions[i]:\n",
    "                continue\n",
    "            count = 0\n",
    "            for e in expeditions[i].split('->'):\n",
    "                if e not in seen:\n",
    "                    seen.add(e)\n",
    "                    count += 1\n",
    "            if count > max_val:\n",
    "                max_val = count\n",
    "                res = i \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, a: List[str]) -> int:\n",
    "        vis = set(a[0].split('->'))  # 这样可能会把空串插入，但是没有关系\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(a)):\n",
    "            if a[i] == \"\": continue\n",
    "            cnt = 0\n",
    "            for t in a[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        vis = set(expeditions[0].split('->'))\n",
    "        max_cnt, ans = 0, -1\n",
    "        for i in range(1, len(expeditions)):\n",
    "            if expeditions[i] == '': continue\n",
    "            cnt = 0\n",
    "            for t in expeditions[i].split('->'):\n",
    "                if t not in vis:\n",
    "                    vis.add(t)\n",
    "                    cnt += 1\n",
    "            if cnt > max_cnt:\n",
    "                max_cnt, ans = cnt, i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def adventureCamp(self, expeditions: List[str]) -> int:\n",
    "        start = set(expeditions[0].split(\"->\"))\n",
    "        if expeditions[0].find(\"->\") < 0:\n",
    "            start.clear()\n",
    "            if expeditions[0] != \"\": start.add(expeditions[0])\n",
    "        idx, num = -1, 0\n",
    "        for i in range(1, len(expeditions)):\n",
    "            cur = set(expeditions[i].split(\"->\"))\n",
    "            if expeditions[i].find(\"->\") < 0:\n",
    "                cur.clear()\n",
    "                if expeditions[i] != \"\": cur.add(expeditions[i])\n",
    "            for ch in start:\n",
    "                if ch in cur: cur.remove(ch)\n",
    "            for ch in cur: start.add(ch)\n",
    "            if len(cur) > num:\n",
    "                num = len(cur)\n",
    "                idx = i\n",
    "        return idx\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
